package engine.ui.equipStrenGthen 
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.table.NxTable;
	import newx.com.control.text.NxLabel;
	/**
	 * ...
	 * @author ...WangHaiBo 
	 * ...........www.woniu.com
	 * 装备升级
	 */
	public class EquipUpLogic implements IEquipLogic 
	{
		private static var _self			:EquipUpLogic;
		private var viewgroup_left_5		:NxViewGroup;
		private var table_left_5			:NxTable;
		private var lab_succeedpercent_5	:NxLabel;
		private var lab_needequiplevel_5	:NxLabel;
		private var lab_losenumber_5		:NxLabel;
		private var lab_needmoney_5			:NxLabel;
		private var lab_getmoney_5			:NxLabel;
		private var btn_star_up_5			:NxButton;
		private var btn_star_down_5			:NxButton;
		private var btn_cd_5				:NxButton;
		private var _bindManage				:BindManage;
		private var _model					:ModelLocator;
		
		private var inforLenght				:int	=	6;
		/**升级后信息表*/
		private  var _equipUpgradePro		:Array	;
		/**当前已强化属性*/
		private  var _currentUpgradePro		:Array	;
		/**降级收益*/
		private var _downgradeIncome		:Number;
		private var _upgradeFailedIncome	:Number;
		/**成功率*/
		private var _baseSuccessRate		:Number
		/**当前强化装备*/
		private var _currentUpgradeEquip	:BaseObject;
		/**当前等级*/
		private var _currentLevel			:int	=	0;
		/**下一级*/
		private var _nextLevel				:int;
		/**装备需求的等级（服务器发送）**/
		private var _needEquipLevel			:int;
		/**装备等级(本地读取)**/
		private var _equipLevel				:int;
		/**消耗金币**/
		private var _goldCount				:int;
		private var _upCDTimeInt			:int
		private var _upObj:Object
		
		public function EquipUpLogic(val:MovieClip) 
		{
			_bindManage				=	BindManage.getInstance();
			_model					=	ModelLocator.getInstance();
			viewgroup_left_5		=	val.getChildByName("viewgroup_left_5") as NxViewGroup;
			table_left_5			=	val.getChildByName("table_left_5") as NxTable;
			lab_succeedpercent_5	=	val.getChildByName("lab_succeedpercent_5") as NxLabel;
			lab_needequiplevel_5	=	val.getChildByName("lab_needequiplevel_5") as NxLabel;
			lab_losenumber_5		=	val.getChildByName("lab_losenumber_5") as NxLabel;
			lab_needmoney_5			=	val.getChildByName("lab_needmoney_5") as NxLabel;
			lab_getmoney_5			=	val.getChildByName("lab_getmoney_5") as NxLabel;
			btn_star_up_5			=	val.getChildByName("btn_star_up_5") as NxButton;
			btn_star_down_5			=	val.getChildByName("btn_star_down_5") as NxButton;
			btn_cd_5				=	val.getChildByName("btn_cd_5") as NxButton;
			init();
			
		}
		public static function getInstance(dis:MovieClip):EquipUpLogic
		{
			if (_self == null) 
			{
				_self	=	new EquipUpLogic(dis);
			}
			return _self;
		}
		/**
		 * 添加装备
		 * @param	val
		 */
		public function addEquip(val:BaseObject):void 
		{
			var item:MovieClip	=	viewgroup_left_5.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			_currentUpgradeEquip	=	val;
			var equipClip:NxClip	=	new NxClip();
			equipClip.width			=	equipClip.height	=	40;
			equipClip.x				=	6;
			equipClip.y				=	5;
			val.HandleProperties("NowViewType", View.VIEWPORT_EQUIPUPGRADE);
			item["data"]			=	val;
			equipClip.source		=	"res/items/icon_" + val.GetProperties("Photo") + ".jpg";
			item.addChild(equipClip);
			paintEquipPro();
			needLevel	=	_needEquipLevel;
			checkStarDownBtnEnabled();
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_BEST_CD,3);
		}
		/**
		 * 移除装备
		 * @param	val
		 */
		public function removeEquip(val:BaseObject):void 
		{
			removeAll();
		}
		/**
		 * 清空所有显示信息
		 */
		public function removeAll():void 
		{
			var item:MovieClip	=	viewgroup_left_5.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			_currentUpgradeEquip		=	null;
			item["data"]				=	null;
			table_left_5.dataProvider	=	null;
			lab_needmoney_5.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_40", ["", ""]);
			lab_getmoney_5.htmlText		=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_41", ["", ""]);
			lab_succeedpercent_5.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_36", [""]);
			lab_losenumber_5.htmlText		=	"";	
			btn_star_up_5.enabled			=	false;
			btn_star_down_5.enabled			=	false;
			//btn_cd_5.visible				=	false;
		}
		/**
		 * 初始化
		 */
		private function init():void
		{
			//btn_cd_5.visible	=	false;
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GET_EQUIP_UPGRADE_INFO), equipUpgradeHandler);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_DAILYCOUNT), 			getUpNumHandler);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_BEST_CD), 			getMinCTTimer);
			addListent()
		}
		/**
		 * 最小CD时间
		 * @param	e
		 */
		public function CDTimer(val:Object):void
		{
			_upCDTimeInt	=	val["time"];
			_upObj			=	val;
			if (_upCDTimeInt <= 0 ) 
			{
				///btn_cd_5.visible	=	false;
				lab_needequiplevel_5.htmlText	=	"";
				checkStarBtnEnabled();
			}
			else
			{
				//btn_cd_5.visible		=	true;
				btn_star_up_5.enabled	=	false;
				lab_needequiplevel_5.htmlText	=	StaticIni.getSwfLangStrVar("UIStr_1001783", getHMS(_upCDTimeInt));
			}
		}
		/**
		 * 服务器返回信息
		 * @param	e
		 */
		private function equipUpgradeHandler(e:CustomMsgEvent):void
		{
			var dataArray:Array	=	e.pList;
			_equipUpgradePro	=	[];
			_currentUpgradePro	=	[];
			for (var i:int = 0; i < inforLenght; i++) 
			{
				_equipUpgradePro[i]		=	Number(dataArray[2 + i]);
				_currentUpgradePro[i]	=	Number(dataArray[28 + i]);
			}
		
			//最后设等级，如果等级达到最高级，升级数据将失效
			_currentLevel					=	dataArray[0];
			_nextLevel						=	dataArray[1];
			_goldCount						=	dataArray[13];
			_downgradeIncome				=	dataArray[27];
			_needEquipLevel					=	dataArray[40];
			_baseSuccessRate				=	dataArray[8];
			_upgradeFailedIncome			=	dataArray[25];
			paintEquipPro();
			
			upNeedMoney						=	_goldCount;
			downGiveMoney					=	_downgradeIncome;
			succeedPercent					=	_baseSuccessRate;
			needLevel						=	_needEquipLevel;
			givePercent						=	_upgradeFailedIncome;
			checkStarDownBtnEnabled();
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_BEST_CD,3);
		}
		/**
		 * 向表格中写入装备信息
		 */
		private function paintEquipPro():void
		{
			if (_currentUpgradeEquip == null || _currentUpgradePro == null) return 
			table_left_5.dataProvider	=	null;
			//基础属性表
			var tableItemData:Array		=	[];
			//基础属性ID
			var baseProId:String;
			//一条基础属性
			var	 baseProItem:Array;
			//属性名
			var proName		:String;
			//当前属性
			var currentPro	:int;
			//升级后的属性
			var upPro		:int;
			var basePropObj:Object		= StaticIni.getIniObj("AddProp");
			var configItemObj:Object	=	StaticIni.getIniObj(_currentUpgradeEquip.GetProperties("ConfigID"));
			
			tableItemData.push( StaticIni.getSwfLangStr("EquipUpgrade_Level") );
			tableItemData.push( _currentLevel);
			tableItemData.push( _nextLevel < 0?	"-"	:	_nextLevel);
			table_left_5.addLineByIndex(tableItemData, -1,tableItemData);
			
			for (var i:int = 0; i < inforLenght; i++) 
			{
				tableItemData	=	[];
				baseProId		=	configItemObj["AddPropNo" + i];
				if (baseProId 	== null) break;
				baseProItem		=	String(basePropObj[baseProId]).split(",");
				//第4项为属性类型
				if (baseProItem[3] 	== undefined) break;
				proName			=	StaticIni.getSwfLangStr("EquipUpgrade_" + baseProItem[3]);
				tableItemData.push(proName);
				
				currentPro		=	int(baseProItem[4]) + int(String(basePropObj[_currentUpgradePro[i]]).split(",")[4]);
				tableItemData.push(StaticIni.getSwfLangStrVar("UIStr_1001337",[currentPro]));
				
				upPro			=	int(baseProItem[4])+int(String(basePropObj[_equipUpgradePro[i]]).split(",")[4])
				tableItemData.push(StaticIni.getSwfLangStrVar("UIStr_1001337",[upPro]));
				
				table_left_5.addLineByIndex(tableItemData, -1,tableItemData);
			}
		}
		/**
		 * 显示升级需要钱数
		 */
		private function set upNeedMoney(val:Number):void
		{
			var textStr:String		=	"";
			var haveMenoy:Number	=	_model.scene.Role.getGoldCoin();
			var moneyType:String	=	StaticIni.getSwfLangStr("CapitalType_0")
			textStr					=	haveMenoy < val? "<font color='#FF0000'>" + String(val) + "</font>" :"<font color='#00FF40'>" + String(val) + "</font>" 
			lab_needmoney_5.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_40",[textStr,moneyType])
		}
		/**
		 * 降级返还钱数
		 */
		private function set downGiveMoney(val:Number):void
		{
			var textStr:String		=	"";
			var moneyType:String	=	StaticIni.getSwfLangStr("CapitalType_0")
			textStr					=	"<font color='#FF0000'>" + String(val) + "</font>";
			lab_getmoney_5.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_41",[textStr,moneyType])
		}
		/**
		 * 按钮是否可用;
		 */
		private function checkStarBtnEnabled():void
		{
			btn_star_up_5.enabled	=	false;
			if (_currentUpgradeEquip != null) 
			{
				btn_star_up_5.enabled	=	_currentLevel > 40  ? false : true;
			}
			if (_currentUpgradeEquip != null)
			{
				var configItemObj:Object	=	StaticIni.getIniObj(_currentUpgradeEquip.GetProperties("ConfigID"));
				var equipMaxLevel:int		=	configItemObj.IntensityMaxLevel;
				btn_star_up_5.enabled	=	_currentLevel < equipMaxLevel? true: false;
			}
		}
		/**
		 * 降级按钮是否可用
		 */
		private function checkStarDownBtnEnabled():void
		{
			btn_star_down_5.enabled	=	false;
			if (_currentUpgradeEquip != null) 
			{
				btn_star_down_5.enabled	=	_currentLevel <= 0	? false : true;
			}
		}
		/**
		 * 返还比例
		 */
		private function set givePercent(val:Number):void
		{
			var textStr:String		=	"";
			textStr	=	val < 0? "":StaticIni.getSwfLangStrVar("UIStr_1001614", [String("<font color='#00FF40'>" + String(val) + "%</font>")]);
			lab_losenumber_5.htmlText	=	textStr;
		}
		/**
		 * 需要等级
		 */
		private function set needLevel(val:Number):void
		{
			var textStr:String		=	"<font color='#FF0000'>0</font>"
			var equipLevel:int;
			if (_currentUpgradeEquip != null)
			{
				var configItemObj:Object	=	StaticIni.getIniObj(_currentUpgradeEquip.GetProperties("ConfigID"));
				equipLevel		=	configItemObj.NeedLevel;
				textStr			=	equipLevel < val? "<font color='#FF0000'>" + String(val) + "</font>":"<font color='#00FF40'>" + String(val) + "</font>";
			}
			//lab_needequiplevel_5.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_38", [textStr]);
		}
		/**
		 * 成功率
		 */
		private function set succeedPercent(val:Number):void
		{
			var textStr:String		=	"";
			textStr					=	val < 100?"<font color='#FF0000'>" + String(val) + "%</font>" :"<font color='#00FF40'>" + String(val) + "%</font>" 
			lab_succeedpercent_5.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_36", [textStr]);
		}
		/**
		 * 服务器返回信息
		 * @param	e
		 */
		private function getUpNumHandler(e:CustomMsgEvent):void
		{
			
		}
		/**
		 * 获取最少CD时间
		 * @param	e
		 */
		private function getMinCTTimer(e:CustomMsgEvent):void
		{
			if (e.pList[1]>0) 
			{
				btn_star_up_5.enabled	=	false;
				//lab_needequiplevel_5.htmlText	=	"";
			}
			else
			{
				checkStarBtnEnabled();
				//btn_star_up_5.enabled	=	true;
			}
		}
		/**
		 * 获取[时\分\秒] 
		 * @param val 以秒为单位的整数
		 * @return [天\时\分\秒]
		 * 
		 */		
		private function getHMS(val:int):Array
		{
			val	=	val<0	?	0	:	val;
			var h:int	=	int(	val%(60*60*24)/(60*60)		);
			var m:int	=	int(	val%(60*60)	/60				);
			var s:int	=	int(	val%60						);
			return [h,m,s];
		}
		/**
		 * 升降级
		 * @param	e
		 */
		private function starBtnClickHandle(e:MouseEvent):void
		{
			if (e.target == btn_star_up_5) 
			{
				btn_star_up_5.enabled	=	false;
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_EQUIPUPGRADE,100);
			}
			else if (e.target	==	btn_star_down_5)
			{
				btn_star_down_5.enabled	=	false;
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_EQUIPDEUPGRADE);
			}
			else if (e.target	==	btn_cd_5)
			{
			}
		}
		
		private function addListent():void
		{
			btn_star_up_5.addEventListener(MouseEvent.CLICK, starBtnClickHandle);
			btn_star_down_5.addEventListener(MouseEvent.CLICK, starBtnClickHandle);
			btn_cd_5.addEventListener(MouseEvent.CLICK, starBtnClickHandle);
		}
		private function removeListent():void
		{
			btn_star_up_5.removeEventListener(MouseEvent.CLICK, starBtnClickHandle);
			btn_star_down_5.removeEventListener(MouseEvent.CLICK, starBtnClickHandle);
			btn_cd_5.removeEventListener(MouseEvent.CLICK, starBtnClickHandle);
		}
		

		
	}

}